// 数字，英文
export function checkNoAndEng(rule, value, callback) {
  if (!value) {
    return Promise.resolve();
  } else if (!/^[a-zA-Z0-9]/.test(value)) {
    return Promise.reject("请输入英文，数字");
  } else {
    return Promise.resolve();
  }
}

// 数字，英文，中文
export function checkText(rule, value, callback) {
  if (!value) {
    return Promise.resolve();
  } else if (!/^[a-zA-Z0-9\u4e00-\u9fa5]/.test(value)) {
    return Promise.reject("请输入中文，英文，数字");
  } else {
    return Promise.resolve();
  }
}

// 数字，符号
export function checkNoAndSymbol(rule, value, callback) {
  if (!value) {
    return Promise.resolve();
  } else if (!/^[0-9~'!@#￥$%^&*()-+_=:]/.test(value)) {
    return Promise.reject("请输入数字，符号");
  } else {
    return Promise.resolve();
  }
}

// 金额验证保留2位小数
export function checkAmount(rule, value, callback) {
  if (!value) {
    return Promise.resolve();
  } else if (!/^[0-9]+(.[0-9]{1,2})?$/.test(value)) {
    return Promise.reject("请输入正数且最多保留2位小数");
  } else {
    return Promise.resolve();
  }
}

// 邮箱验证
export function checkEmail(rule, value, callback) {
  if (value && !/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(value)) {
    return Promise.reject("请输入正确的邮箱地址");
  } else {
    return Promise.resolve();
  }
}

// 身份证验证
export function checkIdNo(rule, idNo = "", callback) {
  const number = idNo.split("");
  const W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 加权因子
  const ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; // 身份证验证位值.10代表X
  if (!number || number.length !== 18) {
    return callback("请输入正确身份证号");
  }
  if (number[17] === "x" || number[17] === "X") {
    number[17] = 10;
  }
  let sum = 0;
  for (let i = 0; i < 17; i++) {
    sum += W[i] * number[i];
  }
  if (number[17] * 1 !== ValideCode[sum % 11] * 1) {
    console.log(
      `[checkIdNo]: ${idNo} last number should be ${ValideCode[sum % 11]}`
    );
    callback("请输入正确身份证号");
  }
  callback();
}
